//
//  TitleBannerView.m
//  HomeDoorWay
//
//  Created by apple on 17/9/19.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "TitleBannerView.h"

@interface TitleBannerView ()<BannerViewDelegate>
@property (nonatomic , strong) UILabel * titleLabel;
@end

@implementation TitleBannerView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.currentIndex = 0;
        [self ybInitView];
    }
    return self;
}

-(void)ybInitView{
    [self addSubview:self.bannerView];
    UIView * contentView = [[UIView alloc]initWithFrame:CGRectMake(0, ItemPX * 395, [UIScreen zj_screenWith], ItemPX * 100)];
    contentView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
    [self addSubview:contentView];
    [contentView addSubview:self.titleLabel];
}


-(void)initDataSourceWithImageDataArray:(NSArray *)imageDataArray{
    if ([self.imageDataArray isEqualToArray:imageDataArray]) {
        return;
    }
    self.imageDataArray = imageDataArray;
    self.currentIndex = 0;
    self.bannerView.theIndex = self.currentIndex;
    NSMutableArray * tempArray = [NSMutableArray array];
    for (int i = 0; i < self.imageDataArray.count; i ++) {
        [tempArray addObject:[NSString stringWithFormat:@"%@" , self.imageDataArray[i][@"url"]]];
    }
    self.bannerView.imageNameArray = [tempArray copy];
    if (self.imageDataArray.count > 0) {
        self.titleLabel.text = [NSString stringWithFormat:@"%@" , self.imageDataArray[self.currentIndex][@"title"]];
    }
}



-(void)refreshTitleLabel{
    self.titleLabel.text = [NSString stringWithFormat:@"%@" , self.imageDataArray[self.currentIndex][@"title"]];
}


-(void)didClickImageView
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(didClickImageViewWithIndex:)]) {
        [self.delegate didClickImageViewWithIndex:self.currentIndex];
    }
}


-(BannerView *)bannerView
{
    if (!_bannerView) {
        _bannerView = [[BannerView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith] , ItemPX * 505)];
        _bannerView.delegate = self;
        _bannerView.isShowPageControl = YES;
        __weak typeof(self) weakself = self;
        _bannerView.block = ^(NSInteger currentImageIndex){
            weakself.currentIndex = currentImageIndex;
            [weakself refreshTitleLabel];
        };
    }
    return _bannerView;
}


-(UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [ToolClass creatLabelWithFrame:CGRectMake(ItemPX * 15, 0, [UIScreen zj_screenWith] - ItemPX * 280, ItemPX * 100) font:15 textColor:[UIColor whiteColor] text:@""];
        _titleLabel.userInteractionEnabled = NO;
//        _titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    }
    return _titleLabel;
}

@end
